package com.st.vmall.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.st.vmall.util.UploadUtil;
import com.st.vmall.entity.Admin;
import com.st.vmall.entity.JSONResult;
import com.st.vmall.service.IAdminService;
import com.st.vmall.util.ValidateCodeUtil;
@Controller
@RequestMapping("/admin/")
public class AdminController {
	
	@Autowired
	private IAdminService adminService;
	
	/**
	 * 页面跳转
	 * @return
	 */
	@GetMapping("/login.do")
	public String login() {
		return "admin/login";
	}
	@GetMapping(value= {"/index.do","/"})
	public String index() {
		return "admin/index";
	}
	@GetMapping("/logout.do")	//退出登录
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:login.do";
	}
	@GetMapping("/modifyPwd.do")
	public String modifyPwd() {
		return "admin/modifyPwd";	
	}
	@GetMapping("/modify.do")
	public String modify() {
		return "admin/modify";	
	}
	
	
	@PostMapping("/login.do")
	public ModelAndView login(Admin admin, String code,HttpSession session,ModelAndView mv) {
		
		//对比code
		String realCode = (String) session.getAttribute("code");
		if (realCode == null ||  !realCode.equals(code.toUpperCase())) {//realCode == null || 
			
			//提示验证码输错
			mv.addObject("error","验证码错误");
			mv.setViewName("admin/login");
		}else {
			
			//访问service层
			Admin loginAdmin = adminService.login(admin);
			
			if (loginAdmin != null) {
				//登录成功
				session.setAttribute("admin", loginAdmin);
				mv.setViewName("redirect:index.do");
				
			}else {
				//登录失败
				mv.addObject("error","账号或密码错误");
				mv.setViewName("admin/login");
			}
		}
		
		return mv;
	}
	
	
//	@GetMapping(value= {"/NewFile.do","/"})
//	public String NewFile() {
//		return "admin/NewFile";
//	}
	/**
	 * 获取验证码
	 * @param session
	 * @param response
	 */
	@GetMapping("/code.do")
	public void code(HttpSession session,HttpServletResponse response) {
		
		String code = ValidateCodeUtil.createValidateCode(response);
		
		//将生成的字符串保存到Session中
		session.setAttribute("code", code);
		
	}
	
	@PostMapping("/modify.json")
	@ResponseBody
	public JSONResult modify(Admin admin,HttpSession session) {

		Admin admin2 = (Admin) session.getAttribute("admin");
		admin.setId(admin2.getId());
		
		int res = adminService.modify(admin);
		
		JSONResult result = new JSONResult();
		result.setCode(res);
		if (res > 0) {
			
			result.setMessage("修改成功");
			
		}else {
			result.setMessage("修改失败");
		}
		
		return result;
	}
	
	@RequestMapping("/modifyPwd.json")
	@ResponseBody
	public JSONResult modifyPwd(HttpSession session,String oldpassword,String newpassword,String newpassword2) {
		
		Admin admin = (Admin) session.getAttribute("admin");
		
		int res = adminService.modifyPwd(admin,oldpassword,newpassword,newpassword2);
		
		JSONResult result = new JSONResult();
		result.setCode(res);
		if (res > 0) {
			session.invalidate();
			result.setMessage("修改成功");
			
		}else if (res == -2) {
			result.setMessage("旧密码输入错误");
		}else if (res == -3) {
			result.setMessage("两次密码输入不一致");
		}else {
			result.setMessage("未知错误");
		}
		
		return result;
	}
}
